using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Runtime.InteropServices;
using Microsoft.CommerceServer.Interop;
using PIPECONFIGLib;

namespace PipeModifier
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MenuItem menuFile;
		private System.Windows.Forms.MenuItem menuOpen;
		private System.Windows.Forms.MenuItem menuSeperator1;
		private System.Windows.Forms.MenuItem menuExit;
		private System.Windows.Forms.MenuItem menuClose;
		private System.Windows.Forms.MenuItem menuSaveAs;
		private System.Windows.Forms.MenuItem menuSave;
		private System.Windows.Forms.MenuItem menuSeperator2;
		private System.Windows.Forms.MainMenu pipeMainMenu;
		private System.Windows.Forms.OpenFileDialog pipeOpen;
		private System.Windows.Forms.SaveFileDialog pipeSave;
		private System.Windows.Forms.ImageList pipeImages;
		private System.Windows.Forms.MenuItem menuHelp;
		private System.Windows.Forms.MenuItem menuAbout;
		private System.ComponentModel.IContainer components;

		private System.Windows.Forms.TreeView pipetv;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ListView pipelv;
		private System.Windows.Forms.StatusBar pipeStatusBar;
		private System.Windows.Forms.ColumnHeader chKey;
		private System.Windows.Forms.ColumnHeader chValue;
		private System.Windows.Forms.ContextMenu pipeContextMenu;
		private System.Windows.Forms.MenuItem componentPropertiesmenuItem;
		
		private bool needToSave = false;
		private string pcfFile = string.Empty;
		private System.Windows.Forms.ContextMenu compContextMenu;
		private System.Windows.Forms.MenuItem beforeMenuItem;
		private System.Windows.Forms.MenuItem afterMenuItem;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem insertCompBefore;
		private System.Windows.Forms.MenuItem insertCompAfter;
		private System.Windows.Forms.MenuItem insertStage;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem deleteMenuItem;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private PIPECONFIGLib.PipeManagerClass pcfManager = new PIPECONFIGLib.PipeManagerClass();

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.pipeMainMenu = new System.Windows.Forms.MainMenu();
			this.menuFile = new System.Windows.Forms.MenuItem();
			this.menuOpen = new System.Windows.Forms.MenuItem();
			this.menuClose = new System.Windows.Forms.MenuItem();
			this.menuSeperator1 = new System.Windows.Forms.MenuItem();
			this.menuSave = new System.Windows.Forms.MenuItem();
			this.menuSaveAs = new System.Windows.Forms.MenuItem();
			this.menuSeperator2 = new System.Windows.Forms.MenuItem();
			this.menuExit = new System.Windows.Forms.MenuItem();
			this.menuHelp = new System.Windows.Forms.MenuItem();
			this.menuAbout = new System.Windows.Forms.MenuItem();
			this.pipeOpen = new System.Windows.Forms.OpenFileDialog();
			this.pipeSave = new System.Windows.Forms.SaveFileDialog();
			this.pipeImages = new System.Windows.Forms.ImageList(this.components);
			this.pipeStatusBar = new System.Windows.Forms.StatusBar();
			this.pipetv = new System.Windows.Forms.TreeView();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.pipelv = new System.Windows.Forms.ListView();
			this.chKey = new System.Windows.Forms.ColumnHeader();
			this.chValue = new System.Windows.Forms.ColumnHeader();
			this.pipeContextMenu = new System.Windows.Forms.ContextMenu();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.insertCompBefore = new System.Windows.Forms.MenuItem();
			this.insertCompAfter = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.componentPropertiesmenuItem = new System.Windows.Forms.MenuItem();
			this.compContextMenu = new System.Windows.Forms.ContextMenu();
			this.insertStage = new System.Windows.Forms.MenuItem();
			this.afterMenuItem = new System.Windows.Forms.MenuItem();
			this.beforeMenuItem = new System.Windows.Forms.MenuItem();
			this.deleteMenuItem = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// pipeMainMenu
			// 
			this.pipeMainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuFile,
																						 this.menuItem1,
																						 this.menuHelp});
			// 
			// menuFile
			// 
			this.menuFile.Index = 0;
			this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuOpen,
																					 this.menuClose,
																					 this.menuSeperator1,
																					 this.menuSave,
																					 this.menuSaveAs,
																					 this.menuItem6,
																					 this.menuSeperator2,
																					 this.menuExit});
			this.menuFile.Text = "&File";
			// 
			// menuOpen
			// 
			this.menuOpen.Index = 0;
			this.menuOpen.Text = "&Open";
			this.menuOpen.Click += new System.EventHandler(this.menuOpen_Click);
			// 
			// menuClose
			// 
			this.menuClose.Index = 1;
			this.menuClose.Text = "&Close";
			this.menuClose.Click += new System.EventHandler(this.menuClose_Click);
			// 
			// menuSeperator1
			// 
			this.menuSeperator1.Index = 2;
			this.menuSeperator1.Text = "-";
			// 
			// menuSave
			// 
			this.menuSave.Index = 3;
			this.menuSave.Text = "&Save";
			this.menuSave.Click += new System.EventHandler(this.menuSave_Click);
			// 
			// menuSaveAs
			// 
			this.menuSaveAs.Index = 4;
			this.menuSaveAs.Text = "Save As...";
			this.menuSaveAs.Click += new System.EventHandler(this.menuSaveAs_Click);
			// 
			// menuSeperator2
			// 
			this.menuSeperator2.Index = 6;
			this.menuSeperator2.Text = "-";
			// 
			// menuExit
			// 
			this.menuExit.Index = 7;
			this.menuExit.Text = "E&xit";
			this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
			// 
			// menuHelp
			// 
			this.menuHelp.Index = 2;
			this.menuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.menuAbout});
			this.menuHelp.Text = "&Help";
			// 
			// menuAbout
			// 
			this.menuAbout.Index = 0;
			this.menuAbout.Text = "&About";
			this.menuAbout.Click += new System.EventHandler(this.menuAbout_Click);
			// 
			// pipeImages
			// 
			this.pipeImages.ImageSize = new System.Drawing.Size(16, 16);
			this.pipeImages.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("pipeImages.ImageStream")));
			this.pipeImages.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// pipeStatusBar
			// 
			this.pipeStatusBar.Location = new System.Drawing.Point(0, 283);
			this.pipeStatusBar.Name = "pipeStatusBar";
			this.pipeStatusBar.Size = new System.Drawing.Size(488, 22);
			this.pipeStatusBar.TabIndex = 1;
			this.pipeStatusBar.Text = "statusBar1";
			// 
			// pipetv
			// 
			this.pipetv.Dock = System.Windows.Forms.DockStyle.Left;
			this.pipetv.ImageList = this.pipeImages;
			this.pipetv.Indent = 19;
			this.pipetv.Location = new System.Drawing.Point(0, 0);
			this.pipetv.Name = "pipetv";
			this.pipetv.Size = new System.Drawing.Size(208, 283);
			this.pipetv.TabIndex = 2;
			this.pipetv.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pipetv_MouseDown);
			this.pipetv.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.pipetv_AfterSelect);
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(208, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 283);
			this.splitter1.TabIndex = 3;
			this.splitter1.TabStop = false;
			// 
			// pipelv
			// 
			this.pipelv.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					 this.chKey,
																					 this.chValue});
			this.pipelv.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pipelv.FullRowSelect = true;
			this.pipelv.Location = new System.Drawing.Point(211, 0);
			this.pipelv.Name = "pipelv";
			this.pipelv.Size = new System.Drawing.Size(277, 283);
			this.pipelv.TabIndex = 4;
			this.pipelv.View = System.Windows.Forms.View.Details;
			// 
			// chKey
			// 
			this.chKey.Text = "Key";
			this.chKey.Width = 114;
			// 
			// chValue
			// 
			this.chValue.Text = "Value";
			this.chValue.Width = 156;
			// 
			// pipeContextMenu
			// 
			this.pipeContextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																							this.menuItem2,
																							this.menuItem3,
																							this.deleteMenuItem,
																							this.menuItem4,
																							this.componentPropertiesmenuItem});
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 0;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.insertCompBefore,
																					  this.insertCompAfter});
			this.menuItem2.Text = "Insert Components";
			// 
			// insertCompBefore
			// 
			this.insertCompBefore.Index = 0;
			this.insertCompBefore.Text = "Before";
			this.insertCompBefore.Click += new System.EventHandler(this.insertCompBefore_Click);
			// 
			// insertCompAfter
			// 
			this.insertCompAfter.Index = 1;
			this.insertCompAfter.Text = "After";
			this.insertCompAfter.Click += new System.EventHandler(this.insertCompAfter_Click);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 1;
			this.menuItem3.Text = "-";
			// 
			// componentPropertiesmenuItem
			// 
			this.componentPropertiesmenuItem.Index = 4;
			this.componentPropertiesmenuItem.Text = "Modify Properties";
			this.componentPropertiesmenuItem.Click += new System.EventHandler(this.componentPropertiesmenuItem_Click);
			// 
			// compContextMenu
			// 
			this.compContextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																							this.insertStage});
			// 
			// insertStage
			// 
			this.insertStage.Index = 0;
			this.insertStage.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						this.afterMenuItem,
																						this.beforeMenuItem});
			this.insertStage.Text = "Insert Stage";
			// 
			// afterMenuItem
			// 
			this.afterMenuItem.Index = 0;
			this.afterMenuItem.Text = "After";
			this.afterMenuItem.Click += new System.EventHandler(this.afterMenuItem_Click);
			// 
			// beforeMenuItem
			// 
			this.beforeMenuItem.Index = 1;
			this.beforeMenuItem.Text = "Before";
			this.beforeMenuItem.Click += new System.EventHandler(this.beforeMenuItem_Click);
			// 
			// deleteMenuItem
			// 
			this.deleteMenuItem.Index = 2;
			this.deleteMenuItem.Text = "Delete...";
			this.deleteMenuItem.Click += new System.EventHandler(this.deleteMenuItem_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 3;
			this.menuItem4.Text = "-";
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 1;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem5});
			this.menuItem1.Text = "&Edit";
			// 
			// menuItem5
			// 
			this.menuItem5.Enabled = false;
			this.menuItem5.Index = 0;
			this.menuItem5.Text = "Delete...";
			this.menuItem5.Click += new System.EventHandler(this.deleteMenuItem_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 5;
			this.menuItem6.Text = "&Refresh";
			this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(488, 305);
			this.Controls.Add(this.pipelv);
			this.Controls.Add(this.splitter1);
			this.Controls.Add(this.pipetv);
			this.Controls.Add(this.pipeStatusBar);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.pipeMainMenu;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Pipe Modifier";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void menuOpen_Click(object sender, System.EventArgs e)
		{
			// close the current file first if any
			menuClose_Click(sender, e);

			// open the file dialog
			pipeOpen.Filter = "Pipeline Configuration file (*.pcf)|*.pcf|All files (*.*)|*.*";

			pipeOpen.ShowDialog(this);
			pcfFile = pipeOpen.FileName;
			if(pcfFile != string.Empty)
			{
				// load the file 
				pcfManager.Load(pcfFile);

				// load the tree
				loadPipeTree();
			}
		}

		private void menuExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
			Application.Exit();
		}

		private void menuClose_Click(object sender, System.EventArgs e)
		{
			menuItem5.Enabled = false;
			pipelv.Items.Clear();
			pipetv.Nodes.Clear();
			pcfFile = string.Empty;
		}

		private void menuAbout_Click(object sender, System.EventArgs e)
		{
			About frmAbout = new About();
			frmAbout.ShowDialog(this);
		}

		private void menuSaveAs_Click(object sender, System.EventArgs e)
		{
			pipeSave.Filter = "Pipeline Configuration file (*.pcf)|*.pcf|All files (*.*)|*.*";
			pipeSave.ShowDialog(this);
			pcfManager.Save(pipeSave.FileName , 1);
		}

		private void loadPipeTree()
		{
			menuItem5.Enabled = true;
			pipetv.Nodes.Clear();

			pipetv.BeginUpdate();

			// get the pipe configuration
			PIPECONFIGLib.IDictionary pipeDict = pcfManager.GetPipe();
			
			// set the status bar
			pipeStatusBar.Text = pcfFile;

			// load the tree
			TreeNode rootNode = pipetv.Nodes.Add(pipeDict["name"].ToString());
			rootNode.Tag = pipeDict;
			rootNode.ImageIndex = 0;
			rootNode.SelectedImageIndex = 0;

			// get the stages
			getStages(pipeDict, rootNode);
			
			pipetv.Update();
			pipetv.EndUpdate();
		}

		private void getStages(PIPECONFIGLib.IDictionary dict, TreeNode node)
		{
			IEnumerator enumerator = ((PIPECONFIGLib.ISimpleList)dict["Stages"]).GetEnumerator();
			int i = 0;
			while (enumerator.MoveNext()) 
			{
				TreeNode stageNode = node.Nodes.Add(((PIPECONFIGLib.IDictionary)enumerator.Current)["Name"].ToString());
				stageNode.Tag = (PIPECONFIGLib.IDictionary)enumerator.Current;

				if(i==0)
				{
					stageNode.ImageIndex = 1;
					stageNode.SelectedImageIndex =1;
				}
				else
				{
					stageNode.ImageIndex = 2;
					stageNode.SelectedImageIndex = 2;
				}
				
				// now the get components in the stages
				getCommonents((PIPECONFIGLib.ISimpleList)((PIPECONFIGLib.IDictionary)enumerator.Current)["Components"], stageNode);
				i++;
			}
		}

		private void getCommonents(PIPECONFIGLib.ISimpleList list, TreeNode node)
		{
			IEnumerator enumerator = list.GetEnumerator();
			while(enumerator.MoveNext())
			{
				TreeNode compNode = node.Nodes.Add(((PIPECONFIGLib.IDictionary)enumerator.Current)["Name"].ToString());
				compNode.Tag = ((PIPECONFIGLib.IDictionary)enumerator.Current); //["Object"];

				compNode.TreeView.Indent = 1; // ShowPlusMinus

				if(((PIPECONFIGLib.IDictionary)enumerator.Current)["Flags"].ToString() == "0")
				{
					compNode.ImageIndex = 3;
					compNode.SelectedImageIndex = 3;
				}
				else
				{
					compNode.ImageIndex = 4;
					compNode.SelectedImageIndex = 4;
				}

			}
		}

		private void pipetv_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			pipelv.Items.Clear();
			// Set the view to show details.
			pipelv.View = View.Details;
			pipelv.GridLines = true;

			if(e.Node.Tag!=null)
			{
				// check to see what type of object we are dealing with
				PIPECONFIGLib.IDictionary dict = e.Node.Tag as PIPECONFIGLib.IDictionary;
				if (dict != null)
				{
					dumpDictionary(dict);
				}
			}

		}

		private void dumpDictionary(PIPECONFIGLib.IDictionary dict)
		{
			foreach(string d in dict)
			{
				ListViewItem item = new ListViewItem(d);
				item.SubItems.Add(dict[d.ToString()].ToString());
				pipelv.Items.Add(item);
			}
		}

		private void pipetv_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			// create context menu
			if(e.Button == MouseButtons.Right) 
			{
				Point point = new Point(e.X, e.Y);
				// set the current node as selected
				TreeNode node = pipetv.GetNodeAt(point);
				// dumb ass bug sometime if you click quick enough on the root node you will get a null
				if(node!=null)
				{
					node.TreeView.SelectedNode = node;
					
					// now that we have the right mouse click see if it's the node we want
					int i = pipetv.SelectedNode.ImageIndex;
					switch(i)
					{
						case 3:
						case 4:
							// now we need to know if we have any dictionary objects to show
							IPipelineComponentAdmin obj = ((PIPECONFIGLib.IDictionary)pipetv.SelectedNode.Tag)["Object"] as IPipelineComponentAdmin;
							if(obj != null ) //&& ((PIPECONFIGLib.IDictionary)obj.GetConfigData()).Count > 0)
							{
								pipeContextMenu.Show(pipetv, point);
							}
							break;
						case 1:
						case 2:
							compContextMenu.Show(pipetv, point);
							break;
					}
				}
			}

		}

		private void componentPropertiesmenuItem_Click(object sender, System.EventArgs e)
		{
			// make a DataTable so we can show the value in the next form
			DataTable dt = new DataTable("Table1");
			dt.Columns.Add("Key");
			dt.Columns.Add("Value");

			IPipelineComponentAdmin obj = ((PIPECONFIGLib.IDictionary)pipetv.SelectedNode.Tag)["Object"] as IPipelineComponentAdmin;
			if(obj!=null)
			{
				PIPECONFIGLib.IDictionary dict = (PIPECONFIGLib.IDictionary)obj.GetConfigData();
				// get the key pair values
				foreach(string d in dict)
				{
					DataRow dr = dt.NewRow();
					dr["Key"] = d;
					dr["Value"] = dict[d].ToString();
					dt.Rows.Add(dr);
				}

				PipeProperties pipeProp = new PipeProperties(dt);
				pipeProp.ShowDialog(this);

				// iterate through the table and set values
				// dict = null;
				foreach(DataRow dr in pipeProp.GetTable.Rows)
				{
					// only update if we have a match
					foreach(string d in dict)
					{
						if(d==dr["Key"].ToString())
						{
							dict[d] = dr["Value"].ToString();
						}
					}
				}

				obj.SetConfigData(dict);
				needToSave = true;
			}

		}

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// as if the user wants to save the changes if we have any
			if(needToSave==true)
			{
				DialogResult result = MessageBox.Show("Do you want to save your changes!", "PipeModifier", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
				if(result==DialogResult.Yes)
				{
					pcfManager.Save(pcfFile, 1);
					needToSave = false;
				}
			}
		}

		private void menuSave_Click(object sender, System.EventArgs e)
		{
			pcfManager.Save(pcfFile, 1);
			needToSave = false;
		}

		private void beforeMenuItem_Click(object sender, System.EventArgs e)
		{
			// insert stage before
			// get the selected noe and insert a new stage
			int i = pipetv.SelectedNode.Index;
			pcfManager.CreateStage(Guid.NewGuid().ToString("B").ToUpper(), "New Stage", i);
			
			loadPipeTree();

			pipetv.SelectedNode = pipetv.Nodes[0].Nodes[i];
			pipetv.SelectedNode.Expand();
			needToSave = true;
		}

		private void afterMenuItem_Click(object sender, System.EventArgs e)
		{
			// insert stage after
			// get the selected noe and insert a new stage
			int i = pipetv.SelectedNode.Index;
			if(i==0)
			{
				i=1;
			}
			else
			{
				i++;
			}

			pcfManager.CreateStage(Guid.NewGuid().ToString("B").ToUpper(), "New Stage", i);
			loadPipeTree();

			pipetv.SelectedNode = pipetv.Nodes[0].Nodes[i];
			pipetv.SelectedNode.Expand();
			needToSave = true;
			
		}

		private void insertCompBefore_Click(object sender, System.EventArgs e)
		{
			// insert Comp
			ChooseComp compProp = new ChooseComp(pcfManager, ((PIPECONFIGLib.IDictionary)pipetv.SelectedNode.Parent.Tag)["clsid"].ToString(), pipetv.SelectedNode.Parent.Text, "Components");
			compProp.ShowDialog(this);

			CreateComponent(compProp.itemResualts, false);
		}

		private void insertCompAfter_Click(object sender, System.EventArgs e)
		{
			// insert comp
			ChooseComp compProp = new ChooseComp(pcfManager, ((PIPECONFIGLib.IDictionary)pipetv.SelectedNode.Parent.Tag)["clsid"].ToString(), pipetv.SelectedNode.Parent.Text, "Components");
			compProp.ShowDialog(this);

			CreateComponent(compProp.itemResualts, true);
		}

		private void CreateComponent(ListViewItem itemResualts, bool After)
		{
			// find out the stage index and the component index
			int i = pipetv.SelectedNode.Parent.Index; //stage;
			int j = pipetv.SelectedNode.Index; //component;
			if(After==true)
			{
				j++;
			}
			else
			{
				if(j!=0)
				{
					j--;
				}
			}

			pcfManager.CreateComponent(itemResualts.Tag.ToString(), itemResualts.Text, i, j);
			loadPipeTree();

			// select the parent node to expand
			pipetv.SelectedNode = pipetv.Nodes[0].Nodes[i].Nodes[j];
			pipetv.SelectedNode.Expand();
			needToSave = true;
		}

		private void deleteMenuItem_Click(object sender, System.EventArgs e)
		{
			int i = pipetv.SelectedNode.ImageIndex;
			switch(i)
			{
				case 3:
				case 4:
					pcfManager.DeleteComponent(pipetv.SelectedNode.Parent.Index, pipetv.SelectedNode.Index);
					pipetv.SelectedNode.Remove();
					needToSave = true;
					break;
				case 1:
				case 2:
					pcfManager.DeleteStage(pipetv.SelectedNode.Index);
					pipetv.SelectedNode.Remove();
					needToSave = true;
					break;
			}
		}

		private void menuItem6_Click(object sender, System.EventArgs e)
		{
			loadPipeTree();		
		}
	}
}
